//
//  GSPlatform.m
//  GSEngine
//
//  Created by Гукун Александр on 1/22/14.
//  Copyright (c) 2014 SNK. All rights reserved.
//

#include "Precompiled.h"
#include "iOSPlatform.h"
#include "iOSWindow.h"
#include "iOSRenderSystem.h"
#include "iOSAppDelegate.h"

namespace GSEngine
{
    iOSPlatform::iOSPlatform(int argc, char**argv):m_argc(argc), m_argv(argv)
    {
        m_resourcesFolderPath = [[NSString stringWithFormat:@"%@", [[NSBundle mainBundle] bundlePath]] UTF8String];
        std::cout << "IOS platform created\n";
    }
    
    iOSPlatform::~iOSPlatform()
    {
        std::cout << "IOS platform destroyed\n";
    }

    GSWindow *iOSPlatform::createWindow(size_t width, size_t height)
    {
        int pointWidth;
        int pointHeight;
        
        if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"] ||
            [[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
        {
            pointWidth = 1024;
            pointHeight = 768;
        }
        else
        {
            pointWidth = 480;
            pointHeight = 320;
        }
        
        CGRect screenBounds = [UIScreen mainScreen].bounds;
        
        m_window = new iOSWindow(vec2f(1024,768), vec2f(screenBounds.size.width, screenBounds.size.height));
        m_renderSystem = new iOSRenderSystem();
        m_renderSystem->initWithWindow(screenBounds.size.width, screenBounds.size.height);
        
        return m_window;
    }
    
    int iOSPlatform::startLoop()
    {
        int retVal = UIApplicationMain(getArgc(),
                                       getArgv(),
                                       nil,
                                       NSStringFromClass([iOSAppDelegate class]));
        return retVal;
    }
    
    int iOSPlatform::getArgc()const
    {
        return m_argc;
    }
    
    char **iOSPlatform::getArgv()const
    {
        return m_argv;
    }
// not class methods
    GSPlatform* CreatePlatformIOS(int argc, char* argv[])
    {
        return new iOSPlatform(argc, argv);
    }

}